<template>
  <!--  计量单位和货品分类管理-->
  <div class="page">
    <div class="page_main">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="货品分类" name="type">
          <div style="height: 80vh;padding: 20px">
            <el-tag
              :key="tag.id"
              v-for="tag in typeList"
              closable
              :disable-transitions="false"
              @close="handleDelete(tag.id,'type')">
              {{tag.name}}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm('type')"
              @blur="handleInputConfirm('type')">
            </el-input>
            <el-button v-else class="button-new-tag" size="small" type="primary" icon="el-icon-plus" @click="showInput">
              添加
            </el-button>
          </div>
        </el-tab-pane>
        <el-tab-pane label="计量单位" name="unit">
          <div style="height: 80vh;padding: 20px">
            <el-tag
              :key="tag.id"
              v-for="tag in unitList"
              closable
              :disable-transitions="false"
              @close="handleDelete(tag.id,'unit')">
              {{tag.name}}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm('unit')"
              @blur="handleInputConfirm('unit')">
            </el-input>
            <el-button v-else class="button-new-tag" size="small" type="primary" icon="el-icon-plus" @click="showInput">
              添加
            </el-button>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  addGoodsUnit,
  addGoodsType,
  getGoodsType,
  getGoodsUnit,
  delGoodsUnit,
  delGoodsType,
} from '@/services/ERP/goods'
import ItemMixins from "../item.mixins";
// form表单校验规则
export default {
  mixins: [ItemMixins],
  data() {
    return {
      activeName: 'type',
      // 表单校验的规则
      rules: {
        name: {required: true, message: '请输入名称'}
      },
      isShowDialog: false, // 是否显示弹窗
      form: {},//货品类型
      typeList: [],
      unitList: [],
      type: '',//表单类型，unit/type
      dialogTitle: '',
      inputVisible: false,
      inputValue: '',
    }
  },
  methods: {
    async submitDelete(id) {
      let action = this.type === 'unit' ? delGoodsUnit : delGoodsType
      let res = await action({id: id})
      if (res.code === 200) {
        this.$notify({
          type: 'success',
          title: '提示',
          message: '删除成功！'
        })
        if (this.type === 'unit') {
          this.getGoodsUnitList()
        } else {
          this.getGoodsTypeList()
        }
      }
    },
    handleDelete(id, type) {
      this.type = type
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(id)
      }).catch(() => {

      });
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm(type) {
      this.type = type
      if (this.inputValue) {
        let cnName = this.inputValue
        if (this.type === 'unit') {
          this.$prompt('请输入' + cnName + '的英文简写', '提示', {
            confirmButtonText: '可以了',
            cancelButtonText: '不用',
          }).then(({value}) => {
            this.submit(cnName, value)
          }).catch(() => {
            this.submit(cnName, '')
          })
        } else {
          this.submit()
        }
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    //获取货品分类
    async getGoodsTypeList() {
      let res = await getGoodsType();
      this.typeList = res.data || []
    },
    //获取计量单位
    async getGoodsUnitList() {
      let res = await getGoodsUnit();
      this.unitList = res.data || []
    },
    //////////////////////////////////////货品类别相关操作开始
    async submit(cnName, unName) {
      let action = this.type === 'unit' ? addGoodsUnit : addGoodsType
      let form = {
        name: this.inputValue,
        cnName: cnName,
        unName: unName,
      }
      let res = await action(form)
      if (res.code === 200) {
        this.$notify({
          type: 'success',
          title: '提示',
          message: '添加成功！'
        })
        if (this.type === 'unit') {
          this.getGoodsUnitList()
        } else {
          this.getGoodsTypeList()
        }
      }
    },
    //////////////////////////////////////货品类别相关操作开始
  },
  mounted() {
    this.getGoodsTypeList()
    this.getGoodsUnitList()
  },
}
</script>

<style lang="less" scoped>
  .el-tag {
    margin: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  /deep/ .el-tabs__item.is-active {
    color: #fdfdfd;
    background-color: #228AF5;
  }
  /deep/ .el-tabs__item {
    height: 50px;
    line-height: 50px;
    font-size: 20px;
  }
</style>
